/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.*;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class EditorialDAO {
    
    private PreparedStatement pStmtSelectByCategory = null;
    private PreparedStatement pStmtSelectCount = null;
    private PreparedStatement pStmtSelectAll = null;
    private PreparedStatement pStmtSelect = null;
    private PreparedStatement pStmtInsert = null;
    private PreparedStatement pStmtDelete = null;
    private PreparedStatement pStmtUpdate = null;
    private static EditorialDAO editorialDAO = null;

    public static EditorialDAO singleton() {
        if (editorialDAO == null) {
            editorialDAO = new EditorialDAO();
        }
        return editorialDAO;
    }

    public EditorialDAO() {
        try {
            pStmtSelectCount = DBConnection.getConnection().prepareStatement("SELECT Count(ArticleId) FROM Editorials");
            pStmtSelectByCategory = DBConnection.getConnection().prepareStatement("SELECT * FROM Editorials WHERE EditorialCatId=?");
            pStmtSelectAll = DBConnection.getConnection().prepareStatement("SELECT * FROM Editorials LIMIT ?,?");
            pStmtSelect = DBConnection.getConnection().prepareStatement("SELECT * FROM Editorials WHERE ArticleId = ?");
            pStmtInsert = DBConnection.getConnection().prepareStatement("INSERT INTO Editorials(EditorialCatId, ItemId, ArticleTitle, ArticleDesc)"
                    + " VALUES(?,?,?,?)");
            pStmtDelete = DBConnection.getConnection().prepareStatement("DELETE FROM Editorials WHERE ArticleId = ?");
            pStmtUpdate = DBConnection.getConnection().prepareStatement("UPDATE Editorials SET EditorialCatId = ?"
                    + ",ItemId = ?,ArticleTitle = ?,ArticleDesc = ? WHERE ArticleId = ?");
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int recordCount(){
        int totalRecord = 0;
        try {
            ResultSet rs = pStmtSelectCount.executeQuery();
            while (rs.next()) {
                totalRecord = rs.getInt(1);
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CardTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return totalRecord;
    }

    public Editorial selectByCategory(int editorialCatId) {
        Editorial editorial = null;
        try {
            pStmtSelectByCategory.setInt(1, editorialCatId);
            ResultSet rs = pStmtSelectByCategory.executeQuery();
            if (rs.next()) {
                editorial= new Editorial(rs.getInt(1), rs.getInt(2),
                        rs.getInt(3), rs.getString(4), rs.getString(5));
            }
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return editorial;
    }

    public ArrayList<Editorial> selectAll(int page) {
        ArrayList<Editorial> lstEditorials = new ArrayList<Editorial>();
        int offset = (page - 1) * WebConst.ROW_PER_PAGE;
        try {
            pStmtSelectAll.setInt(1, offset);
            pStmtSelectAll.setInt(2, WebConst.ROW_PER_PAGE);
            ResultSet rs = pStmtSelectAll.executeQuery();
            while (rs.next()) {
                lstEditorials.add(new Editorial(rs.getInt(1), rs.getInt(2),
                        rs.getInt(3), rs.getString(4), rs.getString(5)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lstEditorials;
    }

    public Editorial select(int articleId) {
        Editorial editorial = null;
        try {
            pStmtSelect.setInt(1, articleId);
            ResultSet rs = pStmtSelect.executeQuery();
            while (rs.next()) {
                editorial = new Editorial(rs.getInt(1), rs.getInt(2),
                        rs.getInt(3), rs.getString(4), rs.getString(5));
            }
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return editorial;
    }

    public String insert(Editorial editorial) {
        String result = "";
        boolean bRet = false;
        int i = 1;
        try {
            pStmtInsert.setInt(i++, editorial.getEditorialCatId());
            pStmtInsert.setInt(i++, editorial.getItemId());
            pStmtInsert.setString(i++, editorial.getArticleTitle());
            pStmtInsert.setString(i++, editorial.getArticleDesc());
            pStmtInsert.execute();
            bRet = true;
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = bRet ? "Successed" : "Failed";
        return result;
    }

    public String delete(int articleId) {
        String result = "";
        int numOfRows = 0;
        try {
            pStmtDelete.setInt(1, articleId);
            numOfRows = pStmtDelete.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows > 0 ? "Successed" : "Failed";

        return result;
    }

    public String update(Editorial editorial) {
        String result = "";
        int numOfRows = 0;
        int i = 1;
        try {
            pStmtUpdate.setInt(i++, editorial.getEditorialCatId());
            pStmtUpdate.setInt(i++, editorial.getItemId());
            pStmtUpdate.setString(i++, editorial.getArticleTitle());
            pStmtUpdate.setString(i++, editorial.getArticleDesc());
            pStmtUpdate.setInt(i++, editorial.getArticleId());
            numOfRows = pStmtUpdate.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(EditorialDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows > 0 ? "Successed" : "Failed";

        return result;
    }
}
